from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from models import *
from config.config_loader import ConfigLoader
from handlers import APIHandler

# Load configuration
config = ConfigLoader().get_config()
handler = APIHandler()

app = FastAPI()

# Configure CORS using settings from config
app.add_middleware(
    CORSMiddleware,
    allow_origins=config["cors"]["origins"],
    allow_credentials=config["cors"]["allow_credentials"],
    allow_methods=config["cors"]["allow_methods"],
    allow_headers=config["cors"]["allow_headers"],
)

@app.get("/")
async def root():
    return {"message": "Welcome to FastAPI Proxy Service"}

# 1.1 难辨别样本上传
@app.post("/api/updateSamples")
async def update_samples(request: UpdateSamplesRequest) -> BaseResponse:
    return await handler.update_samples(request)

# 1.2 云侧模型增量学习
@app.post("/api/couldEvolution")
async def could_evolution(request: CouldEvolutionRequest) -> BaseResponse:
    return await handler.could_evolution(request)

# 1.3 云边分布式协同进化
@app.post("/api/CollaborateEvolution")
async def collaborate_evolution(request: CollaborateEvolutionRequest) -> BaseResponse:
    return await handler.collaborate_evolution(request)

# 1.4 获取任务进度
@app.post("/api/taskProgress")
async def task_progress(request: TaskProgressRequest) -> BaseResponse:
    return await handler.task_progress(request)

# 2.1 云边协同策略配置
@app.post("/api/strategy")
async def set_strategy(request: StrategyRequest) -> BaseResponse:
    return await handler.set_strategy(request)

# 2.2 边侧推理-单样本推理
@app.post("/api/inference")
async def inference(request: InferenceRequest) -> BaseResponse:
    return await handler.inference(request)

# 2.3 云边分布式推理加速-多样本推理
@app.post("/api/inferenceBatch")
async def inference_batch(request: BatchInferenceRequest) -> BaseResponse:
    return await handler.inference_batch(request)

# 2.4 获取资源占用率
@app.post("/api/gpustatus")
async def gpu_status() -> BaseResponse:
    return await handler.gpu_status()

@app.post("/api/cpustatus")
async def cpu_status() -> BaseResponse:
    return await handler.cpu_status()

@app.post("/api/networkstatus")
async def network_status() -> BaseResponse:
    return await handler.network_status()

# 2.5 获取推理数据
@app.post("/api/getFileByPath")
async def get_file_by_path(request: GetFileRequest) -> BaseResponse:
    return await handler.get_file_by_path(request)